<template>
	<view class="photoBox">
		<view class="addBox" @click="see()" v-if="imageUrl">
			<image  class="addImage" :src="imageUrl"></image>
			<image @click.native.stop="deleteImage()" v-if="!canSee" class="commonSingleBoxImage"
				src="../static/cyry.png"></image>
		</view>
		<view class="addBox" @click="chosePhoto" v-if="!imageUrl && !canSee">
			<image class="image" src="../static/tjfh.png"></image>
		</view>
	</view>
</template>

<script>
	import {
		getToken
	} from '../api/byfz';
	import {
		config
	} from '../config/config';
	import {
		getMessionId,
		preView
	} from '../utils/util';
	export default {
		props: {
			value: {
				default: null,
				type: String
			},
			canSee: {
				default: false
			},
			index:{
				default: -1
			}
		},
		data() {
			return {
				imageUrl: this.value
			};
		},
		methods: {
			updateProp(value) {
				this.imageUrl = value
				this.$emit('updateUpload',this.imageUrl,this.index)
			},
			deleteImage() {
				this.updateProp('')
			},
			see() {
				preView(this.imageUrl, 'image', '')
			},
			uploadImage(filePath) {
				uni.uploadFile({
					url: config.apiBaseUrl + 'common/upload/',
					filePath: filePath,
					name: 'file',
					fileType: "image", //ZFB必填,不然报错
					header: {
						Authorization: getToken()
					},
					success: (result) => {
						const obj = JSON.parse(result.data);
						if (obj.code === 200) {
							this.updateProp(obj.url)
						}
					},
					fail: (err) => {
						uni.showToast({
							title: '网络异常，上传失败!',
							icon: 'error'
						})
					},
				});
			},
			chosePhoto() {
				uni.chooseImage({
					count: 4,
					sizeType: ['original', 'compressed'],
					sourceType: ['camera', 'album'], //这要注意，camera掉拍照，album是打开手机相册
					success: (res) => {
						const tempFilePaths = res.tempFilePaths;
						for (let item of tempFilePaths) {
							this.uploadImage(item)
						}
					},
					file: (res) => {
						uni.showToast({
							title: '网络异常，上传失败!',
							icon: 'error'
						})
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.photoBox {
		display: flex;
		flex-wrap: wrap;
		padding-right: 20rpx;
		padding-left: 40rpx;
	}

	.addBox {
		width: 151rpx;
		height: 151rpx;
		background: #DADADA;
		border-radius: 7rpx 7rpx 7rpx 7rpx;
		opacity: 1;
		border: 0rpx solid #FFFFFF;
		position: relative;
		z-index: 1;
		margin-right: 20rpx;

		.addImage {
			width: 151rpx;
			height: 151rpx;
		}

		.commonSingleBoxImage {
			position: absolute;
			right: -18rpx;
			top: -18rpx;
			width: 37rpx;
			height: 37rpx;
			background: #C9C9C9;
			border-radius: 37rpx;
			opacity: 1;
			border: 0rpx solid #FFFFFF;
		}

		.image {
			position: absolute;
			left: 50rpx;
			right: 50rpx;
			top: 50rpx;
			bottom: 50rpx;
			width: 51rpx;
			height: 51rpx;
			z-index: 2;
		}
	}
</style>